package com.fz.song.controller;

import com.fz.song.common.Constants;
import com.fz.song.enums.ExceptionEnum;
import com.fz.song.exception.CustomException;
import com.fz.song.pojo.SysResource;
import com.fz.song.service.ResourceService;
import com.fz.song.util.DateUtils;
import com.fz.song.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("resourcecontroller")
public class ResourceController extends BaseController {

    @Autowired
    public ResourceService resourceService;

    @RequestMapping("resources")
    public String resources(HttpServletRequest request, HttpServletResponse response) {
        List<SysResource> list = resourceService.queryResource();
        request.setAttribute("datalist", list);
        //页面参数
        return "resource/resources";
    }

    @RequestMapping("resources_getResource")
    @ResponseBody
    public Object resources_getResource(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        String sysid = request.getParameter("sysid");
        int id = NumberUtil.Str2Int(sysid, -1);
        if (id == -1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        SysResource resource = resourceService.getResourceById(id);
        if (resource == null)
            return result(0, "查询失败");
        return result(1, "查询成功", resource);
    }

    @RequestMapping("resources_saveResource")
    @ResponseBody
    public Object resources_saveResource(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        String sysidStr = request.getParameter("sysid");
        String sysResourceName = request.getParameter("sysResourceName");
        String type = request.getParameter("type");
        String url = request.getParameter("url");
        String pidStr = request.getParameter("pid");
        String sortStr = request.getParameter("sort");

        int sysid = NumberUtil.Str2Int(sysidStr, -1);
        int pid = NumberUtil.Str2Int(pidStr, -1);
        int sort = NumberUtil.Str2Int(sortStr, -1);

        if (sysid == -1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }

        SysResource sysResource = null;
        if (sysid == 0) {
            sysResource = new SysResource();
            sysResource.setSysid(0);
            sysResource.setCreatetime(DateUtils.getTimeNow());
        } else {
            sysResource = resourceService.getResourceById(sysid);
            sysResource.setUpdatetime(DateUtils.getTimeNow());
        }
        sysResource.setSysResourceName(sysResourceName);
        sysResource.setSort(sort);
        sysResource.setType(type);
        if (Constants.MENU_MENU.equals(type)) {
            sysResource.setPid(pid);
            sysResource.setUrl(url);
        } else {
            sysResource.setUrl("");
            sysResource.setPid(0);
        }
        int num = resourceService.saveResource(sysResource);
        if (num == 1)
            return result(1, "保存成功");
        return result(0, "保存失败");

    }

}
